import 'babel-polyfill';
import React from 'react';
import 'whatwg-fetch';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import App from './App';
import { message } from 'antd';
import './index.css';
import store from './redux/index';
import axios from 'axios';

message.config({
  maxCount: 1
});

axios.interceptors.request.use(function (config) {
  store.dispatch({
    type: 'changemessage',
    message: 'loading'
  });
  return config;
}, function (err) {
  return Promise.reject(err);
});

axios.interceptors.response.use(response => {
  if (response) {
    store.dispatch({
      type: 'changemessage',
      message: ''
    });
    // console.log(response.data)
    switch (response && response.data && response.data.code) {
      case 40101: // 与后台约定登录失效的返回码,根据实际情况处理
        store.dispatch({ type: 'loginout' }); // 删除用户登录信息
        message.error('登录信息已过期，请刷新重新登录！');
    }
    return response;
  }
}, error => {
  store.dispatch({
    type: 'changemessage',
    message: ''
  });
  message.error('网络异常，请稍后重试！');
  return Promise.reject(error.response);  // 返回接口返回的错误信息
});

ReactDOM.render(<Provider store={store}><App /></Provider>, document.getElementById('root'));
